﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Linq;
using mmoss.configuration;
using mmoss.orm;


namespace mmoss.application.classes
{
    public class posCart
    {
        private List<displayItem> _cartItems = new List<displayItem>();
        private List<displayItem> _taxItems = new List<displayItem>();
        public decimal discountDollar { get; set; }
        public decimal discountPerc { get; set; }
        public string discountCode { get; set; }
        private decimal _cartTotal;
        private decimal _cartTotalOrig;
        private Cart _dbCart;
        private decimal _subTotalCalculated;
        public decimal SubTotalCalculated
        {
            get { return _subTotalCalculated; }
            set { _subTotalCalculated = value; }

        }
        public  decimal TotalTax
        {
            get
            {
                //get the total tax
                return (from objs in TaxItems
                        select objs.originalPrice).Sum();
            }
        }
        public Cart DbCart
        {
            get{return _dbCart;}
        }

        public decimal CartTotal
        {
            get { return _cartTotal; }
            set { _cartTotal = value; }
        }
        public decimal CartTotalOrig
        {
            get { return _cartTotalOrig; }
            set { _cartTotalOrig = value; }
        }
        public void addItem(CartItem l_item)
        {
            //add new new item from source
            /*
            CartItems.Add(new displayItem(l_item.displayItem
                , l_item
                , l_item.itemPrice
                , l_item.itemPrice
                , l_item.Quantity
                , l_item.itemExtendedPrice
                ));
            recalcCart();
             * */
        }

        public posCart(Cart l_cart)
        {
            //set the cart
            _dbCart=l_cart;
            //set the current items
            foreach(CartItem l_item in l_cart.CartItems)
            {
                addItem(l_item);
            }
            //get taxItems
            
            foreach(LocationTax l_taxItem in dbConn.db.LocationTaxs.Where(p=>p.locationID==Default.CurrentLocation.Id).ToList())
            {
                TaxItems.Add(new displayItem(l_taxItem.mmcTaxDesc + " " +l_taxItem.mmcTaxRate.ToString() + "%"
                    ,null
                    ,l_taxItem.mmcTaxRate
                    ,0
                    ,0
                    ,l_taxItem.mmcTaxRate
                    ));
            }
            recalcCart();
            
        }
        public void saveCart()
        {
            //save back to db
        }
        public List<displayItem> CartItems
        {
            get{return _cartItems;}
            set{_cartItems=value;}
        }

        
        public List<displayItem> TaxItems
        {
            get{return _taxItems;}
            set{_taxItems=value;}
        }

        

        public void recalcCart()
        {
            //recalculate based on bound items
            _cartTotal = (from objs in CartItems
                                 select objs.CartSubTotalDisplay).Sum();

            //save to current subtotal
            SubTotalCalculated = _cartTotal;
            //take adjustments
            _cartTotal -= discountDollar;

            //calculate tax
            foreach (displayItem l_item in TaxItems)
            {
                l_item.CartSubTotalDisplay = Math.Round(CartTotal * l_item.originalPrice*decimal.Parse(".01"),2);
            }
            //add the tax
            _cartTotal += (from objs in TaxItems 
                          select objs.CartSubTotalDisplay).Sum();
        }
        public void checkoutCart()
        {
            //checkout to cash register
        }
    }
    public class displayItem
    {
        public string displayName{get;set;}
        public CartItem OriginalItem{get;set;}
        public decimal itemPrice{get;set;}
        public decimal originalPrice { get; set; }
        public decimal Quantity{get;set;}
        public decimal CartSubTotalDisplay{get;set;}
        public displayItem(string l_displayName
            ,CartItem l_originalItem
            ,decimal l_originalPrice
            ,decimal l_itemPrice
            ,decimal l_quantity
            ,decimal l_CartSubTotalDisplay
            )
        {
            displayName=l_displayName;
            OriginalItem=l_originalItem;
            originalPrice = l_originalPrice;
            itemPrice=l_itemPrice;
            Quantity=l_quantity;
            CartSubTotalDisplay=l_CartSubTotalDisplay;
        }
    }
}
